package work823;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class work3Server {
  public static void main(String[] args) {
    ServerSocket serverSocket = null;
    ObjectInputStream ois = null;
    InputStream is = null;
    OutputStream os = null;
    try {
      serverSocket = new ServerSocket(8888);
      System.out.println("服务器已启动，等待客户端连接请求");
      Socket socket = serverSocket.accept();
      is = socket.getInputStream();
      os = socket.getOutputStream();
      ois = new ObjectInputStream(is);
      work3User user = (work3User) ois.readObject();
      if (user.getUsername().equals("bjsxt") && user.getPassword().equals("bjsxt")) {
        System.out.println("连接成功");
        os.write("登陆成功".getBytes());
        os.flush();
      } else {
        System.out.println("用户名或密码有误");
        os.write("用户名或密码有误".getBytes());
        os.flush();
      }
      socket.shutdownOutput();;
      System.out.println(socket.getInetAddress().getHostAddress() + ":" + socket.getPort() + "已连接");
      socket.shutdownInput();
    } catch (Exception e) {
      e.printStackTrace();
    }finally {
      try {
        ois.close();
        serverSocket.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}
